package com.longsec.qq.impl;

import java.util.Map;

import org.apache.http.Header;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.message.BasicHeader;
import org.springframework.stereotype.Service;

import com.longsec.qq.ILoginService;
import com.longsec.qq.common.Constant;
import com.longsec.qq.pojo.Rsp;
import com.longsec.qq.utils.LoginUtil;
import com.longsec.qq.utils.RequestUtil;
import com.longsec.qq.vo.QrInfoVo;
import com.longsec.qq.vo.VerifyVo;
import com.longsec.qrcode.QRCodeUtil;

@Service
public class LoginServiceImpl implements ILoginService {

	@Override
	public QrInfoVo loginQr() {
		try {
			return ptqrShow(xLogin());
		} catch (Exception e) {
			throw new RuntimeException("获取QQ登入二维码异常",e);
		}
	}
	
	@Override
	public QrInfoVo updateQr(Map<String, String> cookies) {
		try {
			return ptqrShow(cookies);
		} catch (Exception e) {
			throw new RuntimeException("刷新QQ登入二维码异常",e);
		}
	}

	@Override
	public VerifyVo verify(String ptLoginSig, String ptQrtoken, Map<String, String> cookies) {
		try {
			return ptqrLogin(ptLoginSig, ptQrtoken, cookies);
		} catch (Exception e) {
			throw new RuntimeException("验证QQ登入二维码异常",e);
		}
	}
	
	
	/**
	 * 首次登入信息
	 * @return Cookies
	 * @throws Exception 
	 */
	private Map<String, String> xLogin() throws Exception{
		String urlXlogin = Constant.getUrlXlogin();
		HttpGet xLoginGet = RequestUtil.getHttpGet(urlXlogin);
		Rsp rsp = RequestUtil.execute(xLoginGet);
		Map<String, String> cookies = rsp.cookies;
		LoginUtil.addCookie(cookies,"pgv_pvi", "9258585088");
		LoginUtil.addCookie(cookies,"pgv_si", "s445362176");
		LoginUtil.addCookie(cookies,"_qpsvr_localtk", "0."+System.currentTimeMillis());
		LoginUtil.addCookie(cookies,"qrsig", "FxaITo4y-58MI5mP9vqFh6O5BTDRRrtFxsLr*FjDdjz-1BPwBduu*XxchNlr4CGW");
		return cookies;
	} 
	
	/**
	 * 检测二维码状态
	 * @param ptLoginSig
	 * @param ptQrtoken
	 * @param cookies
	 * @return
	 * @throws Exception
	 */
	private VerifyVo ptqrLogin(String ptLoginSig, String ptQrtoken, Map<String, String> cookies) throws Exception{
		String cookies2 = LoginUtil.getCookies(cookies);
		String urlXlogin = Constant.getUrlXlogin();
		Header[] headers = new Header[]{
				new BasicHeader("Referer",urlXlogin),
				new BasicHeader("Cookie",cookies2),
		};
		String urlPtqrlogin = Constant.getUrlPtqrlogin(ptQrtoken, ptLoginSig);
		HttpGet ptQrLoginGet =  RequestUtil.getHttpGet(urlPtqrlogin,headers);
		Rsp rsp = RequestUtil.execute(ptQrLoginGet);
		String respBody = new String(rsp.body,"UTF-8");
		System.err.println(respBody);
		if (respBody.indexOf("二维码认证中")>0) {
			return VerifyVo.verify();
		}else if (respBody.indexOf("二维码未失效")>0) {
			return VerifyVo.defaults();
		}else if (respBody.indexOf("二维码已失效")>0) {
			return VerifyVo.update();
		}else if (respBody.indexOf("本次登录已被拒绝")>0) {
			return VerifyVo.update();
		}else{
			//ptuiCB('0','0','https://ptlogin2.qzone.qq.com/check_sig?pttype=1&uin=2062270009&service=ptqrlogin&nodirect=0&ptsigx=bda9387c16a121147d100322385e1a66d883ab354d6054c7ad94e2b25de8584dd348bcfda60433e164f7c7abd71bf72cc8aebb2e398b275da7593f72758fb157&s_url=https%3A%2F%2Fqzs.qzone.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara%3Dizone&f_url=&ptlang=2052&ptredirect=100&aid=549000912&daid=5&j_later=0&low_login_hour=0&regmaster=0&pt_login_type=3&pt_aid=0&pt_aaid=16&pt_light=0&pt_3rd_aid=0','0','登录成功！', '良禽择木:1W？')
			String[] result = LoginUtil.subMiddle(respBody,"ptuiCB(",")").replace("'", "").split(",");
			VerifyVo success = VerifyVo.success(result[2]);
			success.setName(result[(result.length-1)]);
			String qq = LoginUtil.subMiddle(result[2], "uin=", "&");
			success.setQq(qq);
			return success;
		}
	}
	
	
	/**
	 * 获取登入二维码
	 * @param cookies
	 * @return
	 * @throws Exception
	 */
	private QrInfoVo ptqrShow(Map<String, String> cookies) throws Exception{
		Header[] headers = new Header[]{
				new BasicHeader("Referer",Constant.getUrlXlogin()),
				new BasicHeader("Cookie",LoginUtil.getCookies(cookies)),
		};
		String urlPtqrshow = Constant.getUrlPtqrshow();
		//组装二维码
		HttpGet ptQrShowGet =  RequestUtil.getHttpGet(urlPtqrshow,headers);
		Rsp rsp = RequestUtil.execute(ptQrShowGet);
		cookies.putAll(rsp.cookies);
		QrInfoVo info = new QrInfoVo();
		info.setPtLoginSig(LoginUtil.getCookies(cookies,"pt_login_sig"));
		info.setPtqrtoken(LoginUtil.getPtqrtoken(cookies));
		info.setImg(rsp.body);
		info.setCookies(cookies);
		return info;
	}

	
}
